package c.x.jy.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlUtil {

    /**
     * 从url中匹配虚拟目录（除去协议、域名、端口号和参数部分）
     * @param url
     * @param containProjectName
     *            是否去掉项目名
     * @return
     */
    public static String getURI(String url, boolean containProjectName) {
      //  String regx = "(?:[https|http]?://)(?:(?:\\w+\\.){2,3}|\\w+)(?:\\w+)(?::[0-9]+)?" + (containProjectName ? "(?:/\\w+/)" : "")
       //         + "([^?]*)";
       // System.out.println(url);
        String regx = "^(https?://\\w+\\.+.+/+)(.+\\.(png|jpg|gif|bmp|jpeg)^?)";
      //  String regx = "/^((ht|f)tps?)://([\\w-]+(\\.[\\w-]+)*/)*[\\w-]+(\\.[\\w-]+)*/?(\\?([\\w-\\.,@?^=%&:/~\\+#]*)+)?/;";
       // System.out.println(regx.matches(url));
        Pattern p = Pattern.compile(regx);
        Matcher matcher = p.matcher(url);
        if (matcher.find()) {
            return matcher.group(2);
        }
        return null;
    }
    
    public static void main(String[] args) {
        String path = "https://yuanzheshanggou.oss-cn-beijing.aliyuncs.com/aa/bb/script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg?a=1";
        System.out.println(getURI(path,true));///script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg
        System.out.println(getHost(path));//localhost

       /* path="http://localhost:2829/app/script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg?t=3";
        System.out.println(getURI(path,true));//script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg
        System.out.println(getHost(path));//localhost

        path="http://www.baidu.com:2829/app/script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg?t=3";
        System.out.println(getURI(path,true));///script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg
        System.out.println(getHost(path));//www.baidu.com

        path="http://www.baidu.com:2829/app/script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg?t=3";
        System.out.println(getURI(path,true));//script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg
        System.out.println(getHost(path));//www.baidu.com*/

        path="http://192.168.10.36:2829/app/script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg?t=3";
        System.out.println(getURI(path,true));//script/plugin/ueditor/jsp/upload1/20170918/95421505699068820.jpg
        System.out.println(getHost(path));//192.168.10.36
    }

    /**
     * 匹配域名
     * 如url为http://192.168.10.36:2829/appweb/script/plugin/ueditor/jsp/upload1/20170901/41281504250668218.jpg
     * 返回192.168.10.36
     *
     * @param url
     * @return
     */
    public static String getHost(String url) {
        Pattern p = Pattern.compile("(?:https?://)((\\w+\\.){2,3}\\w+|[a-zA-z0-9]+)(?::[0-9]+)?");
        Matcher matcher = p.matcher(url);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }


}